package com.jing.yao.modules.dict.item.controller;


import com.jing.yao.annotation.Permission;
import com.jing.yao.bean.KeyValue;
import com.jing.yao.bean.ResponseBean;
import com.jing.yao.component.controller.YaoBaseController;
import com.jing.yao.component.service.YaoBaseService;
import com.jing.yao.modules.dict.item.controller.param.DictItemParam;
import com.jing.yao.modules.dict.item.dao.entity.DictItemEntity;
import com.jing.yao.modules.dict.item.dto.DictItemDTO;
import com.jing.yao.modules.dict.item.service.DictItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 字典项
 *
 * @author
 */
@RestController
@RequestMapping("/dict/item")
public class DictItemController extends YaoBaseController<DictItemParam, DictItemEntity, DictItemDTO> {

    @Autowired
    private DictItemService dictItemService;

    @Override
    public YaoBaseService<DictItemParam, DictItemEntity> getService() {
        return dictItemService;
    }

    @Override
    public DictItemEntity getEntity() {
        return new DictItemEntity();
    }

    @Override
    public DictItemDTO getDTO() {
        return new DictItemDTO();
    }


    @Override
    @Permission(enable = false)
    public ResponseBean pageList(DictItemParam param) {
        return super.pageList(param);
    }

    /**
     * 下拉菜单
     *
     * @return
     */
    @GetMapping("/select/{dictCode}")
    public ResponseBean select(@PathVariable("dictCode") String dictCode) {
        List<KeyValue> keyValues = dictItemService.select(dictCode);
        return successBean(keyValues);
    }

    /**
     * 获取多个下拉列表
     *
     * @return
     */
    @PostMapping("/select/dictCodes")
    @Permission(enable = false)
    public ResponseBean selectCodes(@RequestBody DictItemParam dictItemParam) {
        Map<String, List<KeyValue>> result = dictItemService.selectCodes(dictItemParam.getDictCodes());
        return successBean(result);
    }

    /**
     * 获取多个下拉列表
     *
     * @return
     */
    @PostMapping("/map/dictCodes")
    @Permission(enable = false)
    public ResponseBean mapDictCodes(@RequestBody DictItemParam dictItemParam) {
        Map<String, Map<String, String>> result = dictItemService.mapDictCodes(dictItemParam.getDictCodes());
        return successBean(result);
    }

    /**
     * 根据数据字典编码获取对应的条目值
     *
     * @param dictCode
     * @return 返回数据字典对应的条目值
     */
    @GetMapping("/dict/{dictCode}")
    @Permission(enable = false)
    public ResponseBean dictMap(@PathVariable("dictCode") String dictCode) {
        List<DictItemEntity> list = dictItemService.list("dict_code", dictCode);
        Map<String, String> dictItems = list.stream().collect(Collectors.toMap(DictItemEntity::getItemName, DictItemEntity::getItemValue));
        return successBean(dictItems);
    }
}
